package com.android.library.adapter;

import android.content.Context;
import android.opengl.GLU;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.android.library.image.GlideUtil;

import java.io.File;

/**
 * @author xiaoxing
 * Created by pengsheng-2 on 2018/8/29.
 */

public class ViewHolder extends RecyclerView.ViewHolder{
    private SparseArray<View> mViews;
    private View mConverView;
    private Context context;

    public ViewHolder(Context context, View itemView) {
        super(itemView);
        mConverView = itemView;
        mViews = new SparseArray<>();
        this.context = context;
    }

    public static ViewHolder get(Context context,ViewGroup parent,int layoutId){
        View itemView = LayoutInflater.from(context).inflate(layoutId,parent,false);
        return new ViewHolder(context,itemView);
    }

    public static ViewHolder get(Context context,View view){
        return new ViewHolder(context,view);
    }

    public <T extends View> T getView(int viewId){
        View view = mViews.get(viewId);
        if (view == null){
            view = mConverView.findViewById(viewId);
            mViews.put(viewId,view);
        }
        return (T) view;
    }

    /**
     *设置文本
     */
    public ViewHolder setText(int viewId,String s){
        TextView tv = getView(viewId);
        tv.setText(s);
        return this;
    }

    /**
     *本地图片
     */
    public ViewHolder setImageId(int viewId,int resId){
        ImageView imageView = getView(viewId);
        imageView.setImageResource(resId);
        return this;
    }

    /**
     * 加载url图片
     */
    public ViewHolder setImageUrl(int viewId,String url){
        ImageView imageView = getView(viewId);
        GlideUtil.loadRemoteImage(context,url,imageView);
        return this;
    }

    /**
     * 加载url图片
     */
    public ViewHolder setImageUrl(int viewId,String url,int defaultImg){
        ImageView imageView = getView(viewId);
        GlideUtil.loadUrlImage(context,url,imageView,defaultImg,null);
        return this;
    }

    /**
     * 加载圆形图片
     */
    public ViewHolder setImageUrCircle(int viewId,String url){
        ImageView imageView = getView(viewId);
        GlideUtil.loadCircleImage(context,url,imageView);
        return this;
    }

    /**
     * 加载四个圆角图片
     */
    public ViewHolder setImageUrlCircularAll(int viewId,String url, float radius){
        ImageView imageView = getView(viewId);
        GlideUtil.loadCircularAllImage(context,url,imageView,radius);
        return this;
    }

    public ViewHolder setOnClickListener(int viewId,View.OnClickListener listener){
        View view = getView(viewId);
        view.setOnClickListener(listener);
        return this;
    }
}
